home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE08 / BOOTSTRP / ABD_MAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-12  |  6.6 KB  |  247 lines

  1. unit Abd_main;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   IniFiles, Forms, Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Label1: TLabel;
  12.     Image1: TImage;
  13.     Bevel1: TBevel;
  14.     procedure FormActivate(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.     Procedure LaunchDiary;
  18.     Function SetupNetIdapi( const NetProg : string ) : boolean;
  19.     Function SetupLocalIdapi( const LocalIdapiProg : string ) : boolean;
  20.     Procedure SetWinIdapi( szIdapi : string );
  21.     Procedure RunDiary( const LocalFName : string );
  22.     Function NewerVersionFound( const LocalFName, NetFName : string ) : boolean;
  23.     Function CopyNetworkProgram( const LocalFName, NetFName : string ) : boolean;
  24.   public
  25.     { Public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. type
  32.    PBOOLEAN = ^boolean;
  33.  
  34. function EnumFunc( Wnd:HWND; pRunning : PBOOLEAN ): bool; export;
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40. function EnumFunc(Wnd:HWND; pRunning : PBOOLEAN ): bool;
  41. var
  42.    ClassName : array[0..30] of char;
  43. begin
  44.    Result := true;
  45.    GetClassName( Wnd, ClassName, 29 );
  46.    if StrIComp( ClassName, 'TF_ABMAIN' ) = 0 then begin
  47.       pRunning^ := TRUE;
  48.       Result := false;
  49.    end;
  50. end;
  51.  
  52. Function CheckWindows : boolean;
  53. var
  54.    bAlreadyRunning : boolean;
  55. begin
  56.    bAlreadyRunning := FALSE;
  57.    EnumWindows( @EnumFunc, longint( @bAlreadyRunning ) );
  58.    Result := not bAlreadyRunning;
  59. end;
  60.  
  61. procedure TForm1.FormActivate(Sender: TObject);
  62. begin
  63.    if CheckWindows then
  64.       LaunchDiary
  65.    else begin
  66.       ShowMessage( 'Diary Program already running' );
  67.       Close;
  68.    end;
  69. end;
  70.  
  71. Procedure TForm1.SetWinIdapi( szIdapi : string );
  72. var
  73.    Ini : TInifile;
  74.    szWindows : array[ 0..255 ] of char;
  75.    FName : string;
  76. begin {SetWinIdapi}
  77.    GetWindowsDirectory( szWindows, sizeof( szWindows ) -1 );
  78.    FName := StrPas( szWindows ) + '\win.ini';
  79.    Ini := TIniFile.Create( FName );
  80.    try
  81.       Ini.WriteString( 'IDAPI', 'CONFIGFILE01', szIdapi );
  82.    finally
  83.       Ini.Free;
  84.    end;
  85. end;
  86.  
  87. Function TForm1.SetupNetIdapi( const netProg : string ) : boolean;
  88. var
  89.    szNetIdapi : string;
  90. begin {SetupNetIdapi}
  91.    Result := FALSE;
  92.    szNetIdapi := ExtractFilePath( NetProg ) + 'idapi.cfg';
  93.    if fileexists( szNetIdapi ) then begin
  94.       SetWinIdapi( szNetIdapi );
  95.       Result := TRUE;
  96.    end else
  97.       showmessage( 'Error - Network IDAPI file not found' );
  98. end;
  99.  
  100. Function TForm1.SetupLocalIdapi( const LocalIdapiProg : string ) : boolean;
  101. begin {SetupLocalIdapi}
  102.    Result := FALSE;
  103.    if fileexists( LocalIdapiProg ) then begin
  104.       SetWinIdapi( LocalIdapiProg );
  105.       Result := TRUE;
  106.    end else
  107.       showmessage( 'Error - Local IDAPI file not found' );
  108. end;
  109.  
  110. Procedure TForm1.RunDiary( const LocalFName : string );
  111. var
  112.    sz0 : array[ 0..255 ] of char;
  113.    res : integer;
  114. begin
  115.    Res := Winexec( StrPCopy( sz0, LocalFName ), SW_SHOWNORMAL );
  116.    if res < 32 then
  117.       ShowMessage( 'Error ' + IntToStr( Res ) + ' running Diary Program' );  
  118. end;
  119.  
  120. Function TForm1.NewerVersionFound( const LocalFName, NetFName : string ) : boolean;
  121. {Function returns TRUE if Network file is newer than local, or if
  122.  local file is not found, FALSE if the local file is more recent.
  123.  NOTE: the Network file has already been found to exist}
  124. begin {NewerVersionFound}
  125.    if FileExists( LocalFName ) {abdiary.exe on local} then
  126.       Result := FileAge( LocalFName ) <= FileAge( NetFName )
  127.    else
  128.       Result := TRUE;
  129. end;
  130.  
  131. Function TForm1.CopyNetworkProgram( const LocalFName, NetFName : string ) : boolean;
  132. const
  133.    BUFSIZE = 60000;
  134. var
  135.    fin, Fout : File;
  136.    pBuf : pchar;
  137.    iRead : word;
  138. begin {CopyNetworkProgram}
  139.    Result := FALSE;
  140.    {$I+}
  141.    try
  142.       GetMem( pBuf, BUFSIZE );
  143.    except
  144.       ShowMessage( 'Insufficient Memory to copy New Diary Program' );
  145.       exit;
  146.    end;
  147.  
  148.    try
  149.       AssignFile( fin, NetFName );
  150.       AssignFile( fout, LocalFName );
  151.       try
  152.          filemode := fmOpenRead + fmShareDenyWrite;
  153.          reset( fin, 1 );
  154.          try
  155.             filemode := fmOpenWrite + fmShareExclusive;
  156.             rewrite( fout, 1 );
  157.          except
  158.             ShowMessage( 'Unable to update Local Diary Program' );
  159.             exit;
  160.          end;
  161.  
  162.          while not eof( fin ) do begin
  163.             blockread( fin, pBuf^, BUFSIZE , iread );
  164.             blockwrite( fout, pBuf^, iRead );
  165.          end;
  166.          CloseFile( fin );
  167.          CloseFile( fout );
  168.          result := TRUE;
  169.          ShowMessage( 'New Version of Diary Program copied from network' );
  170.       except
  171.          ShowMessage( 'Error copying Diary Program' );
  172.       end;
  173.  
  174.    finally
  175.       Freemem( pBuf, BUFSIZE );
  176.    end;
  177. end;
  178.  
  179. Procedure TForm1.LaunchDiary;
  180. var
  181.    LocalFName, NetFName, LocalIdapiFName : string;
  182.    Ini : TIniFile;
  183.    i, i1 : integer;
  184.    td : tDateTime;
  185.    bLaunch : boolean;
  186. const
  187.    Second : extended = 1.0 / 24 / 60 / 60;
  188. begin {LaunchDiary}
  189.    td := Now;
  190.    repeat
  191.       Application.ProcessMessages;
  192.    until ( Now - td ) > Second;
  193.  
  194.    Ini := TIniFile.Create( ExtractFilePath( Application.ExeName ) +
  195.                         'ABDIARY.INI' );
  196.    try
  197.       with Ini do begin
  198.          LocalFName := ReadString( 'DIR', 'LOCAL',
  199.                                 ExtractFilePath( Application.ExeName ) );
  200.          if LocalFName[ length( LocalFName ) ] <> '\' then
  201.             LocalFName := LocalFName + '\';
  202.          LocalFName := LocalFName + 'abdiary.exe';
  203.  
  204.          NetFName := ReadString( 'DIR', 'NET', 'J:\' );
  205.          if NetFName[ length( NetFName ) ] <> '\' then
  206.             NetFName := NetFName + '\';
  207.          NetFName := NetFName + 'abdiary.exe';
  208.  
  209.          LocalIDapiFName := ReadString( 'DIR', 'LOCALIDAPI', 'C:\IDAPI' );
  210.          if LocalIdapiFName[ length( LocalIdapiFName ) ] <> '\' then
  211.             LocalIdapiFName := LocalIdapiFName + '\';
  212.          LocalIdapiFName := LocalIdapiFName + 'idapi.cfg';
  213.       end;
  214.  
  215.    finally
  216.       Ini.Free;
  217.    end;
  218.  
  219.    bLaunch := TRUE;
  220.    if FileExists( NetFName ) then begin {we are networked}
  221.  
  222.       if NewerVersionFound( LocalFName, NetFName ) then
  223.          bLaunch := CopyNetworkProgram( LocalFName, NetFName );
  224.  
  225.       if bLaunch then
  226.          bLaunch := SetupNetIdapi( NetFName );
  227.  
  228.    end else if FileExists( LocalFName ) then {we are local}
  229.  
  230.       bLaunch := SetupLocalIdapi( LocalIdapiFName )
  231.  
  232.    else begin {we are undone!}
  233.  
  234.       ShowMessage( 'Diary Program not found (neither Local nor Networked)' );
  235.       bLaunch := FALSE;
  236.  
  237.    end;
  238.  
  239.    if bLaunch then
  240.       RunDiary( LocalFname );
  241.  
  242.    Close; {i.e. terminate application}
  243.  
  244. end;
  245.  
  246. end.
  247.